﻿/* ***********************************************
* Author:          陶李林
* Team:            晶奇公司研发部
* Created Time:    2010-7-30 10:39:37
* CopyRight:       晶奇公司版权所有 保留一切权利
* NameSpace:       BF.Common
* Interface:       ResultList
* ***********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BF.Common
{
    [Serializable]
    public class ResultList<T>
    {
        /// <summary>
        /// 构造函数(返回结果默认为true)
        /// </summary>
        public ResultList()
        {
            isSuccess = true;
            errorMsgList = new List<Dictionary<string, object>>();
            ModelList = new List<T>();
        }
        /// <summary>
        /// 返回list泛型列表
        /// </summary>
        public List<T> ModelList { get; set; }
       
        /// <summary>
        /// 返回一个model
        /// </summary>
        public T Model { get; set; }
        /// <summary>
        /// 返回linq结果集
        /// </summary>
        public IQueryable IQueryableList { get; set; }
        /// <summary>
        /// 返回所有条数(用户模块使用的参数)
        /// </summary>
        public int AllCount { get; set; }
        /// <summary>
        /// 分页
        /// </summary>
        public Pager<T> pager { get; set; }
        /// <summary>
        /// 是否成功(默认为false)
        /// </summary>
        public bool isSuccess { get; set; }
        /// <summary>
        /// 受影响行数
        /// </summary>
        public int succCount { get; set; }
        /// <summary>
        /// 异常信息字典
        /// </summary>
        public Dictionary<string, object> errorMsg { get; set; }
        /// <summary>
        /// 异常信息字典组
        /// </summary>
        public List<Dictionary<string, object>> errorMsgList { get; set; }
        /// <summary>
        /// 设置异常信息组
        /// </summary> 
        /// <param name="e"></param>
        /// <returns></returns>
        public List<Dictionary<string, object>> setErrorMsg(Exception e)
        {
            
            errorMsg = new Dictionary<string, object>();
            isSuccess = false;
            errorMsg.Add("Date", DateTime.Now);
            errorMsg.Add("InnerException", e.InnerException);
            errorMsg.Add("Errormsg", e.Message);
            errorMsg.Add("Source", e.Source);
            errorMsg.Add("TargetSite", e.TargetSite);
            errorMsgList.Add(errorMsg);
            return errorMsgList;
        }
        /// <summary>
        /// 返回异常信息
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string ErrMsg = string.Empty;
            int i = 1;
            //若存在错误记录
            if (errorMsgList.Count > 0 && !isSuccess)
            {
                foreach (Dictionary<string, object> listDicErr in errorMsgList)
                {
                    ErrMsg = "异常 " + i++ + ":<br>";
                    ErrMsg += "时间:" + listDicErr["Date"] + "<br>";
                    ErrMsg += "实例:" + listDicErr["InnerException"] + "<br>";
                    ErrMsg += "错误的应用程序或对象名称:" + listDicErr["Errormsg"] + "<br>";
                    ErrMsg += "引发异常的方法:" + listDicErr["InnerException"] + "<br>";
                }
                return ErrMsg;
            }
            if (isSuccess && succCount != 0)
            {
                ErrMsg = "操作成功!<br>影响行数:[" + succCount + "]条";
            }
            else
            {
                ErrMsg = "无修改!";   
            }
            if (!isSuccess)
            {
                ErrMsg = "操作失败!<br>请稍后再试!";
            }
            return ErrMsg;
        }
        
        //返回Dataset
        public DataSet DataSetList { get; set; }

    }
}
